package controller;

import com.fasterxml.jackson.databind.ObjectMapper;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.servlet.http.HttpSession;
import service.StatisticsService;
import service.impl.StatisticsServiceImpl;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.Map;

@WebServlet("/api/statistics/*")
public class StatisticsServlet extends HttpServlet {
    private final ObjectMapper objectMapper = new ObjectMapper();
    private final StatisticsService statisticsService = new StatisticsServiceImpl();

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException {
        response.setContentType("application/json;charset=UTF-8");
        PrintWriter out = response.getWriter();
        Map<String, Object> result = new HashMap<>();

        try {
            String pathInfo = request.getPathInfo();
            HttpSession session = request.getSession();
            String role = (String) session.getAttribute("role");

            if (!"OPERATOR".equals(role)) {
                throw new Exception("只有运营人员可以查看统计数据");
            }

            if (pathInfo == null || "/".equals(pathInfo)) {
                // 获取总体统计数据
                Map<String, Object> overallStats = statisticsService.getOverallStatistics();
                result.put("success", true);
                result.put("data", overallStats);
            } else if ("/applications".equals(pathInfo)) {
                // 获取申请相关统计
                String timeRange = request.getParameter("timeRange"); // daily, weekly, monthly
                Map<String, Object> applicationStats = statisticsService.getApplicationStatistics(timeRange);
                result.put("success", true);
                result.put("data", applicationStats);
            } else if ("/complaints".equals(pathInfo)) {
                // 获取投诉相关统计
                String timeRange = request.getParameter("timeRange");
                Map<String, Object> complaintStats = statisticsService.getComplaintStatistics(timeRange);
                result.put("success", true);
                result.put("data", complaintStats);
            } else if ("/performance".equals(pathInfo)) {
                // 获取客服绩效统计
                String timeRange = request.getParameter("timeRange");
                Map<String, Object> performanceStats = statisticsService.getPerformanceStatistics(timeRange);
                result.put("success", true);
                result.put("data", performanceStats);
            } else if ("/trends".equals(pathInfo)) {
                // 获取趋势分析
                String metric = request.getParameter("metric"); // applications, complaints, satisfaction
                String timeRange = request.getParameter("timeRange");
                Map<String, Object> trendStats = statisticsService.getTrendStatistics(metric, timeRange);
                result.put("success", true);
                result.put("data", trendStats);
            } else {
                throw new Exception("无效的请求路径");
            }
        } catch (Exception e) {
            result.put("success", false);
            result.put("message", e.getMessage());
        }

        out.write(objectMapper.writeValueAsString(result));
        out.flush();
    }
}
